       	<?php 		
		//http://code.stephenmorley.org/php/creating-downloadable-csv-files/
		
		include('../config/database.php');
		$mydbconfig = new DATABASE_CONFIG();
			// First we define the variables to fetch DB Show
			$username = $mydbconfig->default['login'];
			$password = $mydbconfig->default['password'];
			$database = $mydbconfig->default['database'];
			$host = $mydbconfig->default['host'];
			
			//then we connect to the mysql server
			mysql_connect($host,$username,$password);
			
			//connect to database within server, or die means we can show connection error
			@mysql_select_db($database) or die( "Unable to select database");
		
		$query="
			SELECT hosts.name AS hostname, profiles.id_text AS profilename, packages.name AS pkgname, packages.id_text AS pkgid, packages.id AS pkgidnum

			FROM hosts
			INNER JOIN hosts_profiles
			 ON hosts.id = hosts_profiles.host_id
			INNER JOIN profiles
			  ON hosts_profiles.profile_id = profiles.id
			INNER JOIN  packages_profiles
			 ON profiles.id = packages_profiles.profile_id
			INNER JOIN packages
			 ON packages_profiles.package_id = packages.id
			 
			 WHERE packages.enabled=1

			UNION ALL

			SELECT hosts.name AS hostname, profiles.id_text AS profilename, packages.name AS pkgname, packages.id_text AS pkgid, packages.id AS pkgidnum

			FROM hosts
			INNER JOIN profiles
			  ON hosts.mainprofile_id = profiles.id
			INNER JOIN  packages_profiles
			 ON profiles.id = packages_profiles.profile_id
			INNER JOIN packages
			 ON packages_profiles.package_id = packages.id
			 
			 WHERE packages.enabled=1
			 
			 ORDER BY hostname, profilename, pkgname
		";


				
				// output headers so that the file is downloaded rather than displayed
				header('Content-Type: text/csv; charset=utf-8');
				header('Content-Disposition: attachment; filename=data.csv');
				
				// create a file pointer connected to the output stream
				$output = fopen('php://output', 'w');
				
				// output the column headings
				fputcsv($output, array('hostname', 'profilename', 'pkgname', 'pkgid', 'pkgidnum'));
				// loop over the rows, outputting them
				$result=mysql_query($query);
				while ($row = mysql_fetch_assoc($result)) fputcsv($output, $row);

        		unset($query);
		unset($result);
		unset($num);
		unset($i);
		mysql_close();
		?>
